package com.cn.demo.service.impl;

import com.cn.demo.entity.AdminAccounts;
import com.cn.demo.entity.Teacher;
import com.cn.demo.mapper.AdminAccountsMapper;
import com.cn.demo.service.AdminAccountsService;
import com.cn.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@Transactional
public class AdminAccountsServiceImpl implements AdminAccountsService {
    @Autowired
    private AdminAccountsMapper adminAccountsMapper;

    @Override
    public Result login(AdminAccounts adminAccounts) {
        AdminAccounts login = adminAccountsMapper.login(adminAccounts);
        if (login == null) {
            return Result.error("用户名或密码错误");
        } else {
            String token = UUID.randomUUID() + "";
            login.setToken(token);
            if (adminAccountsMapper.updateToken(token, login.getAdminId()) == 1) {
                return  Result.success(login);
            } else {
                return  Result.error();
            }
        }
    }
}